using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Toenda.Foundation.Windows.Forms {
	/// <summary>
	/// Class ExplorerTreeView
	/// </summary>
	public partial class ExplorerTreeView : UserControl {
		private string _startPath;

		// ---------------------------------------------------
		// CONSTRUCTORS
		// ---------------------------------------------------

		/// <summary>
		/// Default Ctor
		/// </summary>
		public ExplorerTreeView() {
			InitializeComponent();

			// Set the TreeView image list to the system image list.
			SystemImageList.SetTVImageList(treeWnd.Handle);
			this.LoadRootNodes();
		}


		// ---------------------------------------------------
		// INTERFACE IMPLEMENTATIONS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROPERTIES
		// ---------------------------------------------------

		/// <summary>
		/// Get the selected path
		/// </summary>
		public string SelectedPath {
			get {
				return this.treeWnd.SelectedNode.FullPath;
			}
		}

		/// <summary>
		/// Get or set the start path
		/// </summary>
		public string StartPath {
			get { return this._startPath; }
			set {
				this._startPath = value;
				this.LoadRootNodes();
			}
		}

		// ---------------------------------------------------
		// EVENTS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PRIVATE MEMBERS
		// ---------------------------------------------------

		/// <summary>
		/// Loads the root TreeView nodes.
		/// </summary>
		private void LoadRootNodes() {
			// Create the root shell item.
			ShellItem m_shDesktop = new ShellItem();

			// Create the root node.
			TreeNode tvwRoot = new TreeNode();
			tvwRoot.Text = m_shDesktop.DisplayName;
			tvwRoot.ImageIndex = m_shDesktop.IconIndex;
			tvwRoot.SelectedImageIndex = m_shDesktop.IconIndex;
			tvwRoot.Tag = m_shDesktop;

			// Now we need to add any children to the root node.
			ArrayList arrChildren = m_shDesktop.GetSubFolders();

			foreach(ShellItem shChild in arrChildren) {
				TreeNode tvwChild = new TreeNode();
				tvwChild.Text = shChild.DisplayName;
				tvwChild.ImageIndex = shChild.IconIndex;
				tvwChild.SelectedImageIndex = shChild.IconIndex;
				tvwChild.Tag = shChild;

				// If this is a folder item and has children then add a place holder node.
				if(shChild.IsFolder && shChild.HasSubFolder) {
					tvwChild.Nodes.Add("PH");
				}

				tvwRoot.Nodes.Add(tvwChild);
			}

			// Add the root node to the tree.
			treeWnd.Nodes.Clear();
			treeWnd.Nodes.Add(tvwRoot);
			tvwRoot.Expand();
		}

		// ---------------------------------------------------
		// PROTECTED MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PUBLIC MEMBERS
		// ---------------------------------------------------
    }
}
